<template>
	<view class="home">
		<view class="topNav">
			<u-tabs :list="navList" @click="clickNav"></u-tabs>
		</view>
		
		<view class="loadingState">
			<u-skeleton rows="3" title loading avatar v-if="loadingState"></u-skeleton>
		</view>
		
		<view class="content">
			<view class="item" v-for="(item,i) in dataList" :key="i">
				<blog-item @delEvent="P_delEvent" :item="item" ></blog-item>
			</view>
		</view>
		
		<view class="edit" @click="gotoEdit">
			<image class="icon" src="/static/edit-one.png"></image>
		</view>
	</view>
</template>

<script>
	const db = uniCloud.database();
	const dbCmd = db.command
	import {
		store,
		mutations
	} from '@/uni_modules/uni-id-pages/common/store.js'
	export default {
		data() {
			return {
				loadingState:true,
				title: 'Hello',
				navList:[
					{
						name:"最新",
						type:"publish_date"
					},
					{
						name:"热门",
						type:"view_count"
					}
				],
				dataList:[],
				navActive:0,
				likeTime:""
			}
		},
		computed: {
			userInfo() {
				return store.userInfo
			},
			hasLogin(){
				return store.hasLogin
			}
		},
		onLoad() {
			this.getData()
		},
		methods: {
			P_delEvent(){
				this.dataList = []
				this.getData()
			},
			clickNav(e){
				console.log(e)
				this.navActive = e.index
				this.loadingState = true
				this.dataList = []
				this.getData()
			},
			gotoEdit(){
				uni.navigateTo({
					url:"/pages/edit/edit"
				})
			},
			getData(){
				let artTemp = db.collection("quanzi_article").where(`delState != true`).field("user_id,publish_date,title,content,description,comment_count,like_count,view_count,picurls").getTemp();
				let userTemp = db.collection("uni-id-users").field("_id,username,nickname,avatar_file").getTemp();
				
				db.collection(artTemp,userTemp).orderBy(this.navList[this.navActive].type,"desc").get()
				.then(async res=>{
					//console.log(res)
					let idArr = []
					let resDataArr = res.result.data
					
					if(this.hasLogin){
						resDataArr.forEach(item =>{
							idArr.push(item._id)
						})
						//console.log(idArr)
						
						let likeRes = await db.collection("quanzi_like").where({
							article_id : dbCmd.in(idArr),
							user_id : uniCloud.getCurrentUserInfo().uid
						}).get()
						
						console.log(likeRes)
						
						likeRes.result.data.forEach(item=>{
							let findIndex = resDataArr.findIndex(find=>{
								return item.article_id == find._id
							})
							resDataArr[findIndex].isLike = true
						})
						
					}
					
					
					this.dataList = resDataArr
					this.loadingState = false
				})
			}
		}
	}
</script>

<style lang="scss">
.home{
	padding: 0 30rpx 0 30rpx;
	.topNav{
		
	}
	.content{
		.item{
				border-bottom: 10rpx solid #f7f7f7;
		}
	}
	.edit{
		height: 100rpx;
		width: 100rpx;
		background-color: #55aa7f;
		border-radius: 50%;
		z-index: 999;
		position: fixed;
		bottom: 100rpx;
		right: 50rpx;
		
		display: flex;
		justify-content: center;
		align-items: center;
		
		box-shadow: 0 0 20rpx #55aa7f;
		.icon{
			width: 50rpx;
			height: 50rpx;
		}
	}
}
</style>
